/*************************************************************************
	> File Name: 009.P1048.采药.cpp
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 二  6/29 14:54:05 2021
 ************************************************************************/

#include <iostream>
using namespace std;

int T, M, t[105], v[105], dp[1005]; 

int main() {
    cin >> T >> M;
    for (int i = 1; i <= M; i++) {
        cin >> t[i] >> v[i];
    }

    for (int i = 1; i <= M; i++) {
        for (int j = T; j >= t[i]; j--) {
            dp[j] = max(dp[j], dp[j - t[i]] + v[i]);
        }
    }

    cout << dp[T] << endl;

    return 0;
}
